package com.zach.common.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import com.zach.common.dao.UserFunctionTreeDao;
import com.zach.common.entity.UserFunctionTree;
import com.zach.common.service.UserFunctionTreeService;
import com.zach.util.StrUtil;
@Service(value="userFunctionTreeService")
@Scope(value="singleton",proxyMode=ScopedProxyMode.INTERFACES)
public class UserFunctionTreeServiceImpl implements UserFunctionTreeService,
		Serializable {
	private static final long serialVersionUID = -2061735454172260937L;
	private UserFunctionTreeDao treeDao;

	public UserFunctionTreeDao getTreeDao() {
		return treeDao;
	}

	@Resource(name = "userFunctionTreeDao")
	public void setTreeDao(UserFunctionTreeDao userFunctionTreeDao) {
		this.treeDao = userFunctionTreeDao;
	}

	@Override
	public void addNode(UserFunctionTree tree) {
		treeDao.addNode(tree);
		String parentId = tree.getParentId();
		tree.setIsLeave("1");
		if(StrUtil.isEmpty(parentId)){
			parentId="root";
			tree.setParentId(parentId);
		}
			
		UserFunctionTree t = treeDao.getNode(parentId);
		tree.setVisible(t.getVisible());
		tree.setParent(t);
		tree.setLevel(t.getLevel()+1);
		
		if(t.getIsLeave().equals("1")){
			t.setIsLeave("0");
			treeDao.updateNodeExt(t);
		}
	}
	
	@Override
	public int delNodesCascade(String nodeId) {
		int i = 0 ;
		//UserFunctionTree tree = treeDao.getNode(nodeId);
		List<UserFunctionTree> trees = treeDao.getTreeListByParentId(nodeId);
		i += delTreeNodes(trees);
		treeDao.delNode(nodeId);
		i++;
		return i;
	}
	protected int delTreeNodes(List<UserFunctionTree> trees) {
		int count = 0;
		if(null!=trees && trees.size()>0){
			for(UserFunctionTree tre:trees){
				List<UserFunctionTree> t2 = treeDao.getTreeListByParentId(tre.getId());
				count+=delTreeNodes(t2);
				treeDao.delNode(tre.getId());
				count++;
				return count;
			}
		}
		return count;
	}
	@Override
	public void updateNode(UserFunctionTree tree) {
		treeDao.updateNode(tree);
	}

	@Override
	public UserFunctionTree getNode(String nodeId) {
		UserFunctionTree tree = treeDao.getNode(nodeId);
		if(!StrUtil.isEmpty(tree.getParentId())){
			UserFunctionTree p = treeDao.getNode(tree.getParentId());
			tree.setParent(p);
		}
		return tree;
	}

	@Override
	public List<UserFunctionTree> getTreeList(UserFunctionTree tree) {
		return treeDao.getTreeList(tree);
	}

	@Override
	public List<UserFunctionTree> getTreeListByUserId(String loginName) {
		return treeDao.getTreeListByUserId(loginName);
	}

	@Override
	public List<UserFunctionTree> getTreeListByRole(String roleId) {
		return treeDao.getTreeListByRole(roleId);
	}

	@Override
	public List<UserFunctionTree> getTreeListByParentId(String parentId) {
		List<UserFunctionTree> trees = treeDao.getTreeListByParentId(parentId);
		return trees;
	}
	
	@Override
	public List<UserFunctionTree> getTreeListAllByParent(UserFunctionTree parentTree) {
		List<UserFunctionTree> trees = treeDao.getTreeListByParentIdAndOther(parentTree);
		fillTree(trees,parentTree.getUserId(),parentTree.getRoleId());
		parentTree.setSubTree(trees);
		return trees;
	}
	
	protected void fillTree(List<UserFunctionTree> trees,String userId,String roleId) {
		if (null != trees && trees.size() > 0) {
			for (UserFunctionTree t : trees) {
				t.setUserId(userId);
				t.setRoleId(roleId);
				fillTree(t);
			}
		}
	}
	/**
	 * 获取全部子节点。
	 * 
	 * @param curTree
	 * @return
	 */
	protected void fillTree(UserFunctionTree curTree) {
		List<UserFunctionTree> trees = treeDao.getTreeListByParentIdAndOther(curTree);
		if (null != trees && trees.size() > 0) {
			curTree.setSubTree(new ArrayList<UserFunctionTree>());

			for (UserFunctionTree t : trees) {
				t.setUserId(curTree.getUserId());
				t.setRoleId(curTree.getUserId());
				fillTree(t);
				curTree.getSubTree().add(t);
			}

		}
	}
	/**
	 * 获取当前功能树的json字符串。
	 * easyui的json树需求一个数组
	 */
	public String generateEasyUIJsonTree(UserFunctionTree tree){
//		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
//		for(UserFunctionTree tree:treeList){
			Map<String, Object> map = new HashMap<String, Object>();
			generateEasyUIChildJsonTree(tree, map);
//			result.add(map);
//		}
		
//		return JSONArray.fromObject(result).toString(2);
		return JSONObject.fromObject(map).toString(2);
	}
	/**
	 * 循环生成当前功能树的json字符串。
	 * @param tree 
	 * @param container 临时保存字符串的java类。
	 */
	protected void generateEasyUIChildJsonTree(UserFunctionTree tree,Map<String, Object> container) {
		/*
	    "id":1,
		"text":"Folder1",
		"iconCls":"icon-ok",
		"checked":true,
		"state":"open",
		children
	 */
		container.put("id", tree.getId());
		container.put("text", "["+tree.getId()+"]"+tree.getName());
		container.put("url", tree.getUrl());
		List<UserFunctionTree> subTree = tree.getSubTree();
		if(null!=subTree&& subTree.size()>0){
			List<Map<String, Object>> childs = new ArrayList<Map<String,Object>>();
			for(UserFunctionTree t : tree.getSubTree()){
				Map<String, Object> m = new HashMap<String, Object>();
				generateEasyUIChildJsonTree(t, m);
				childs.add(m);
			}
			container.put("children", childs);
		}
	}
}
